﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.BaseClass.FSM;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.ATK;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.CommonState;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.Special;
using Assets.Scripts.DL.Enemy.SpriderBoss;
using Assets.Scripts.DL.Mgrs;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.YaSuo {
	public class YasuoController : Controller {
		public GameObject Player;
		public SpriteRenderer SpriteRenderer;

		public AudioSource Voice1;
		public AudioSource Voice2;
		[Header("Cnt")]
		public int CurQCnt = 0;

		[Header("Offset")]
		public Vector2 WindOffset;
		public float ShadowDisFromPlayer = 2;

		[Header("Cd")]
		public int ECd = 2;

		[Header("Speed")]
		public int ESpeed = 20;
		public int WindFlySpeed = 10;
		public int ShadowSpeed = 10;
		public int SLGTDownSpeed = -10;

		[Header("Time")]
		public float ETime = 0.2f;
		public float WindFlyTime = 2f;
		public float EachSLGTWaitTime = 1f;

		protected override void Awake() {
			base.Awake();
			//AudioManager.SetSouceGroup(Voice1, AudioStr.Voice);
			//AudioManager.SetSouceGroup(Voice2, AudioStr.Voice);
		}

		protected override void InitAnimMgr() {
			AddAnim(StateEnum.Idle, "Idle");
			AddAnim(StateEnum.Move, "Move");
			AddAnim(StateEnum.Atk1, "Atk1");
			AddAnim(StateEnum.Atk2, "Atk2");
			AddAnim(StateEnum.SLGT, "SLGT");
			//AddAnim(StateEnum.SLGT2, "SLGT2");
			//AddAnim(StateEnum.SLGT3, "SLGT3");
			AddAnim(StateEnum.E, "E");
			AddAnim(StateEnum.Hurt, "Hurt");
			AddAnim(StateEnum.Die, "Die");
		}

		protected override void InitAtkTimer() {
			//base.InitAtkTimer();
			StateData.AtkCdRecoder = new Dictionary<StateEnum, Timer> {
				{StateEnum.Atk1, new Timer() },
				{StateEnum.Atk2, new Timer() },
				{StateEnum.E, new Timer() },
				//{StateEnum.SLGT, new Timer() },
				//风墙无cd
			};
		}

		protected override void InitStates() {
			//一般状态,借用spiderboss的hfsm，就不重写了
			var commonHfsm = new SpiderBossCommonHFSM(this, StateEnum.CommonHFSM);
			var move = new YasuoMove(this, StateEnum.Move);//和spide逻辑相同重写一个是因为没法传this
			var idle = new Idle(this, StateEnum.Idle);
			commonHfsm.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{StateEnum.Idle, idle},
				{StateEnum.Move, move},
				}
			);

			//Atk
			var atkHFSM = new AtkHFSM(this, StateEnum.AtkHFSM);
			//Atk1-4
			var atk1 = new YasuoCommonAtk(this, StateEnum.Atk1);//旋风在commonatk中
			var atk2 = new YasuoCommonAtk(this, StateEnum.Atk2);
			var e = new YasuoE(this, StateEnum.E);
			var windWall = new YasuoWindWall(this, StateEnum.WindWall);
			var slgt = new YasuoSLGT(this, StateEnum.SLGT);
			atkHFSM.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{ StateEnum.WindWall, windWall},
				{ StateEnum.SLGT, slgt},
				{ StateEnum.Atk1, atk1 },
				{ StateEnum.Atk2, atk2 },
				{ StateEnum.E, e },
			});


			//Special
			//差一个Cure！！！！！！！！！！！！！！！！！！！！！！！！
			var specialHFSM = new EnemySpecialHFSM(this, StateEnum.SpecialHFSM);
			var hurt = new Hurt(this, StateEnum.Hurt);
			var die = new Die(this, StateEnum.Die);
			specialHFSM.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{ StateEnum.Die, die },{StateEnum.Hurt, hurt },
			}
			);

			//Root
			_rootHFSM.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{StateEnum.CommonHFSM,  commonHfsm},
				{StateEnum.AtkHFSM,  atkHFSM},
				{StateEnum.SpecialHFSM, specialHFSM },
				}
			);
		}
		public override bool DetectPlayerRemoteAtk() {

			var pos = GetRealXDirPos(Config.AtkOff);
			var coll = Physics2D.OverlapCircle(pos, Config.AtkDis, GetLayer(LayerStrings.RemoteAtk));
			if (coll != null)
				if (coll.CompareTag(Tags.PlayerRemoteAtk)) {
					return true;
				}
			return false;
		}

		public override Vector3 GetPlayerPos()
			=> Player.transform.position;

		public bool IsPlayerKnockFly() {
			if (Player == null) return false;

			var pdata = Player.GetComponent<PlayerController>().StateData;
			return pdata.IsKnockFly;
		}

		//普攻cd
		protected override bool CheckCd() {
			return StateData.AtkCdRecoder[StateEnum.Atk1].Elapsed > Config.CommonAtkGap
				|| StateData.AtkCdRecoder[StateEnum.Atk2].Elapsed > Config.CommonAtkGap;
		}

		public override bool CheckSpecialCd() {
			if (Player == null) {
				return false;
			}
			//狂风绝息斩/风墙
			if (Player.GetComponent<PlayerController>().StateData.IsKnockFly || DetectPlayerRemoteAtk()) {
				return true;
			}
			//E
			if (StateData.AtkCdRecoder[StateEnum.E].Elapsed > ECd) {
				return true;
			}
			return false;
		}

		private void OnDrawGizmos() {
			Gizmos.DrawWireSphere(transform.position + (Vector3)WindOffset, 1);
		}
	}
}
